function AccountsView(controller, config) {

	var self = this;
	var c = config;
	var accountDialog = new AccountView(c.editDialogDiv, function(account) {
		if(account.id == null) {

		} else {

		}
	}, {
		curManager: c.curManager
	});

	this.init = function() {
		var dateTo = createDatePicker(c.dateFilterDiv, new Date());
		var approvedFilter = el(c.approvedFilterDiv).jqxCheckBox({ checked: false });

		el(c.refreshBtnDiv).jqxButton({ width: 120, height: 26 });
		el(c.refreshBtnDiv).click(function() {
			controller.loadAccounts(controller.bookId, dateTo.value, !approvedFilter.value);
		});
		el(c.createBtnDiv).jqxButton({ width: 120, height: 26 });
		el(c.createBtnDiv).click(function() {
			self.showAccountEditForm(null);
		});
		accountDialog.init();

		$(document).on('contextmenu', function (e) {
			if ($(e.target).parents('.data-table-row').length > 0) {
				return false;
			}
			return true;
		});
		return this;
	};

	this.updateAccounts = function(accounts) {
//		console.log('update accounts');
		var ownAccounts = new Array();
		var loanAccounts = new Array();
		var currencies = ['$', 'E', 'L'];
		$.each(accounts, function(index, account) {
			if(account.own) {
				ownAccounts.push(account);
			} else {
				loanAccounts.push(account);
			}
		});
		this.renderTable(ownAccounts, loanAccounts, currencies);
	};

	this.renderTable = function(ownAccounts, loanAccounts, currencies) {
		var accountMap = {};
		var curCnt = currencies.length;
		var curHeader = '';
		var totalBalance = {};
		var totalInitBalance = {};
		var totalLoan = {};
		$.each(currencies, function(index, cur) {
			curHeader += '<td class="data-table-header-cell">' + cur + '</td>';
			totalInitBalance[cur] = 0;
			totalBalance[cur] = 0;
			totalLoan[cur] = 0;
		});

		var html = '<table class="data-table" align="center">' +
			'<tr class="data-table-header">' +
				'<td class="data-table-header-cell" rowspan="2">Счет</td>' +
				'<td class="data-table-header-cell" rowspan="2">Дата создания</td>' +
				'<td class="data-table-header-cell" colspan="' + curCnt + '">Начальный баланс</td>' +
				'<td class="data-table-header-cell" colspan="' + curCnt + '">Баланс</td>' +
				'<td rowspan="2"></td>' +
			'</tr><tr class="data-table-header">' + curHeader + curHeader + '</tr>' +
		// СВОИ СЧЕТА
			'<tr class="data-table-subheader"><td class="data-table-header-cell" colspan="' + (3 + 2 * curCnt) + '">Счета</td></tr>';
		$.each(ownAccounts, function(index, acc) {
			accountMap[acc.id] = acc;
			html += '<tr class="data-table-row"><td class="data-table-cell">' + acc.name + '</td>' +
						'<td class="data-table-cell">' + formatDate(acc.creationDate) + '</td>';
			var initBalanceHtml = '';
			var balanceHtml = '';
			var initBalance;
			var balance;
			$.each(currencies, function(index, cur) {
				initBalance = acc.getInitialBalance(cur);
				totalInitBalance[cur] += initBalance;
				initBalanceHtml += '<td class="data-table-cell data-table-sum-cell">' + formatSum(initBalance) + '</td>';
				balance = acc.getBalance(cur);
				totalBalance[cur] += balance;
				balanceHtml += '<td class="data-table-cell data-table-sum-cell">' + formatSum(balance) + '</td>';
			});
			html += initBalanceHtml + balanceHtml;
			html += '<td>&nbsp;<input id="accountEditBtn' + acc.id + '" type="button" value="✎" />' +
						'&nbsp;<input id="accountRemoveBtn' + acc.id + '" type="button" value="X" />&nbsp;</td></tr>';
		});
		html += '<tr class="data-table-total"><td class="data-table-cell" colspan="2">Итого</td>';
		var totalInitBalanceHtml = '';
		var totalBalanceHtml = '';
		$.each(currencies, function(index, cur) {
			totalInitBalanceHtml += '<td class="data-table-sum-cell">' + formatSum(totalInitBalance[cur]) + '</td>';
			totalBalanceHtml += '<td class="data-table-sum-cell">' + formatSum(totalBalance[cur]) + '</td>';
		});
		html += totalInitBalanceHtml + totalBalanceHtml + '<td></td></tr>';

		// КОНТРАГЕНТЫ
		html += '<tr class="data-table-subheader"><td class="data-table-header-cell" colspan="' + (3 + 2 * curCnt) + '">Контрагенты</td></tr>';
		$.each(loanAccounts, function(index, acc) {
			accountMap[acc.id] = acc;
			html += '<tr class="data-table-row"><td class="data-table-cell">' + acc.name + '</td>' +
						'<td class="data-table-cell">' + formatDate(acc.creationDate) + '</td>';

			var balanceHtml = '<td class="data-table-cell" colspan="' + curCnt + '"></td>';
			var loan;
			$.each(currencies, function(index, cur) {
				loan = formatSum(acc.getBalance(cur));
				totalLoan[cur] += loan;
				balanceHtml += '<td class="data-table-cell data-table-sum-cell">' + loan + '</td>';
			});
			html += balanceHtml;
			html += '<td>&nbsp;<input id="accountEditBtn' + acc.id + '" type="button" value="✎" />' +
						'&nbsp;<input id="accountRemoveBtn' + acc.id + '" type="button" value="X" />&nbsp;</td></tr>';
		});
		html += '<tr class="data-table-total"><td class="data-table-cell" colspan="2">Итого</td>';
		var totalLoanHtml = '<td class="data-table-cell" colspan="' + curCnt + '"></td>';
		$.each(currencies, function(index, cur) {
			totalLoanHtml += '<td class="data-table-sum-cell">' + formatSum(totalLoan[cur]) + '</td>';
		});
		html += totalLoanHtml;
		html += '<td></td></tr>';
		html += '</table>';

		el(c.tableDiv).html(html);
		$.each(ownAccounts, function(index, acc) {
			el('accountEditBtn' + acc.id).jqxButton().on('click', function() {
				self.showAccountEditForm(accountMap[acc.id]);
			});
			el('accountRemoveBtn' + acc.id).jqxButton().on('click', function() {
				controller.removeAccount(acc.id);
			});
		});
		$.each(loanAccounts, function(index, acc) {
			el('accountEditBtn' + acc.id).jqxButton().on('click', function() {
				self.showAccountEditForm(accountMap[acc.id]);
			});
			el('accountRemoveBtn' + acc.id).jqxButton().on('click', function() {
				controller.removeAccount(acc.id);
			});
		});
	};

	this.showAccountEditForm = function(account) {
//		console.log(account);
		accountDialog.show(account)
	};
};
